"use strict";
class Bundle {
    constructor() {
        this.events = new Map;
    }
    once(event, cb) {
        // 调用一次后就清除
        const fn = (...args) => {
            cb(...args);
            this.off(event, fn);
        };
        this.on(event, fn);
    }
    on(event, cb) {
        var _a;
        if (!this.events.has(event)) {
            this.events.set(event, [cb]);
        }
        else {
            (_a = this.events.get(event)) === null || _a === void 0 ? void 0 : _a.push(cb);
        }
    }
    emit(event, ...args) {
        let eventArr = this.events.get(event) || [];
        for (const fn of eventArr) {
            fn(...args);
        }
    }
    off(event, cb) {
        let eventArr = this.events.get(event) || [];
        if (eventArr.indexOf(cb) !== -1) {
            eventArr === null || eventArr === void 0 ? void 0 : eventArr.splice(eventArr.indexOf(cb), 1);
        }
    }
}
const bus = new Bundle();
let fn = (...args) => {
    console.log('msg', ...args);
};
let fun = (...args) => {
    console.log('once:', ...args);
};
bus.on('msg1', fn);
bus.on('msg1', (...args) => { console.log(...args, 'sb'); });
bus.emit('msg1', 1, 1, 1, 1);
bus.off('msg1', fn);
bus.emit('msg1', 1, 1, 1, 1);
console.log('---------------');
bus.once('once', fun);
bus.emit('once', 'ooo');
bus.emit('once', 'ooo');
// bus.on('msg2', fn)
// bus.emit('msg2', 2, 2, 2, 2)
// bus.on('msg3', fn)
// bus.emit('msg3', 3, 3, 3, 3)
